package com.imooc.ad.controller;

import com.alibaba.fastjson.JSON;
import com.imooc.ad.annotation.IgnoreResponseAdvice;
import com.imooc.ad.client.SponsorClient;
import com.imooc.ad.client.vo.AdPlan;
import com.imooc.ad.client.vo.AdPlanGetRequest;
import com.imooc.ad.vo.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @ClassName SearchController
 * Created by zghgchao 2019/3/26 22:05
 **/
@Slf4j
@RestController
public class SearchController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SponsorClient sponsorClient;

    @IgnoreResponseAdvice // 不使用统一响应
    @PostMapping("/getAdPlanByRibbon")
    public CommonResponse<List<AdPlan>> getAdPlanByRibbon(AdPlanGetRequest request){
        log.info("ad-sponsor: getAdPlanByRibbon -> {}",JSON.toJSON(request));
        return restTemplate.postForEntity(
                "http://eureka-client-ad-sponsor/ad-sponsor/get/adPlan",
                request,
                CommonResponse.class
        ).getBody();
    }

    @IgnoreResponseAdvice // 不使用统一响应
    @GetMapping("/test")
    public CommonResponse<Object> test(){
        log.info("ad-sponsor: test -> {}");
        return restTemplate.getForEntity(
                "http://eureka-client-ad-sponsor/ad-sponsor/get/hello",
                CommonResponse.class
        ).getBody();
    }

    @IgnoreResponseAdvice // 不使用统一响应
    @GetMapping("/test1")
    public CommonResponse<Object> test1(){
        log.info("ad-sponsor: test1 -> {}");
        return sponsorClient.test();
    }
}
